package com.icss.ah.authority.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.authority.service.AuthorityService;
import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.j2ee.util.UUID;

/**
 * 店员权限管理
 *
 */
@Controller
@Scope("prototype")
public class AuthorityAction extends BaseAction {
	
	@Autowired
	private AuthorityService authorityService;
	
	/**
	 * 店员权限管理页面
	 * @return
	 */
	public String toAccountRightPage(){
		return "toAccountRightPage";
	}
	
	/**
	 * 获得当前店铺店员列表
	 */
	public void queryAccountList(){
		request = this.getRequest();
		
		String result = "";
		try {
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId = userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			
			List<Map> accountList = authorityService.queryAccountList(map);
			long accountCount = authorityService.queryAccountCount(map);//查询总记录数
			
			dataGridJson.setRows(accountList);
			dataGridJson.setTotal(accountCount);
			result = JSONObject.fromObject(dataGridJson).toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 获取店铺资源树数据
	 */
	public void getResourceTree(){
		request = getRequest();
		String userCode = getUserCodeFromSession();
		UserInfoBean userInfo = getUserInfo(userCode);
		String shopId = userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String appId = ResourceLoader.getInstance("icss").getString("appId");
		
		//id 异步加载展开树时传到后台的id
		//tarEnt 入口编码，以编码为入口编码的节点作为根节点
		//ifAll 是否一次性展示所有数据，1是 0否
		String id = request.getParameter("id");
		id = id == null ? "" : id;
		String ifAll = request.getParameter("ifAll");
		ifAll = ifAll == null ? "1" : ifAll;
		String accountCode = request.getParameter("accountCode");
		accountCode = accountCode == null ? "" : accountCode;
		SyncSelectTree selectTree = new SyncSelectTree();
		selectTree.setId(id);
		selectTree.setIfAll(ifAll);
		selectTree.setCorpId(corpId);
		selectTree.setShopId(shopId);
		selectTree.setAppId(appId);
		selectTree.setAccountCode(accountCode);
		List<SyncSelectTree> list = authorityService.getResourceTree(selectTree);
		HashMap map = new HashMap();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		map.put("appId", appId);
		map.put("accountCode", accountCode);
		List<Map> checkList = new ArrayList();
		checkList = authorityService.queryCheckUserList(map);
		checkList = checkList == null ? new ArrayList() : checkList;
		for (SyncSelectTree sst : list) {
			sst.setOpen(true);
			if(sst.getIsParent() && "0".equals(sst.getpId())){
				sst.setNocheck(true);
			}
			String sstUuid = sst.getUuid();
			if(!checkList.isEmpty()){
				for (Map m : checkList) {
					String mUuid = m.get("RESOURCE_UUID") == null ? "" : m.get("RESOURCE_UUID").toString();
					if(sstUuid.equals(mUuid)){
						sst.setChecked(true);
					}
				}
			}
		}
		System.out.println("==========list:"+list.size());
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	/**
	 * 账号权限分配
	 * @return
	 */
	public String rigthUserManage(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String appId = ResourceLoader.getInstance("icss").getString("appId");
			String userCode = getUserCodeFromSession();
			UserInfoBean userInfo = getUserInfo(userCode);
			String shopId = userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			
			String accountCode = request.getParameter("accountCode");
			accountCode = accountCode == null ? "" : accountCode;
			String uuid = request.getParameter("uuid");
			uuid = uuid == null ? "" : uuid;
			
			HashMap map = new HashMap();
			map.put("corpId", corpId);
			map.put("appId", appId);
			map.put("shopId", shopId);
			map.put("accountCode", accountCode);
			map.put("uuidStr", uuid);
			
			boolean revl = authorityService.insertRigthUser(map);
			
			jsonText.setSuccess(true);
			jsonText.setMsg("分配成功！");
			
		} catch (Exception e) {
			jsonText.setMsg("分配失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
	}
	
}
